fix(auth): normalize bearer token whitespace for rate limit stability - #1291
Conversation
|
Thanks for taking this on. The PR is necessary: it addresses the explicitly assigned Bearer-token whitespace hardening item from #337, and the core I do not think the current revision is merge-ready yet:
Suggested minimal scope: |
bc8c2c4 to
d0a45a3
Compare
|
Maintainer update — took this to merge-ready via Rebased onto current
Diff is now |
`_extract_bearer_token` returned the raw post-scheme remainder, so the same secret sent as `Bearer sk-abc`, `Bearer sk-abc`, or `Bearer sk-abc ` hashed to three different rate-limit buckets — a client could dodge its own limit just by padding the header, and FastAPI-auth-equivalent values mapped to distinct identities. Strip surrounding whitespace (spaces, tabs, newlines) and treat an all-whitespace token as absent. Pins the bucket-level security contract (auth-equivalent Bearer values → one client id), not only the helper. Assigned Bearer-hardening item from Auth follow-ups (raullenchai#337). Co-Authored-By: Claude Opus 4.8 <[email protected]>
d0a45a3 to
6d87471
Compare
|
Thank you for stepping in to refine the scope and correct the TYPE_CHECKING gate issue. I appreciate the clear feedback and the merge. |
What does this PR do?
.strip()to_extract_bearer_tokeninvllm_mlx/middleware/auth.pyso surrounding whitespace (spaces, tabs, newlines) is normalized and an all-whitespace token is treated as absent.tests/test_config_and_middleware.py: the helper's whitespace edge cases and a bucket-level contract test asserting auth-equivalent Bearer values resolve to one_rate_limit_client_id.Why is this needed?
Closes the "Bearer token whitespace normalization" task from #337.
Without this, a client can dodge its own rate limit just by padding the Bearer token with extra spaces/tabs — the unstripped string hashes to a brand-new bucket identity.
Scope note (maintainer, narrowed for merge)
Rebased onto current
mainand narrowed to the auth fix + tests per review. Dropped two out-of-scope changes from the original revision: theif TYPE_CHECKING:gate onserver_config.py(it broke runtimetyping.get_type_hints(ServerConfig)withNameError: BaseEngine), and threeexcept ImportError: pytest.skipguards on the exception-handler tests (they'd turn a broken/partial MLX install into a green skip and hide packaging regressions). Original authorship preserved on the commit.Test plan
python3.12 -m pytest tests/test_config_and_middleware.py::TestRateLimitClientId -q— 5 passed.ruff check && ruff format --checkon both touched files — clean.Checklist
ruff check && ruff format --check)pr_validate.strip()is reverted